package cn.momoky.ccool.controller;

import cn.momoky.ccool.pojo.Chunk;
import cn.momoky.ccool.pojo.FileMD5;
import cn.momoky.ccool.pojo.FileNode;
import cn.momoky.ccool.pojo.UploadTask;
import cn.momoky.ccool.service.FileMD5Service;
import cn.momoky.ccool.service.FileNodeService;
import cn.momoky.ccool.service.FileService;
import cn.momoky.ccool.service.UploadTaskService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/f")
@AllArgsConstructor
public class FileController {

    private final FileService fileService;
    private final FileMD5Service fileMD5Service;
    private final FileNodeService fileNodeService;
    private final UploadTaskService uploadTaskService;

    @ResponseBody
    @PostMapping("/create-upload-task")
    public Map<String, Object> createUploadTask(@RequestBody UploadTask uploadTask) {

        uploadTaskService.add(uploadTask);
        Map<String, Object> map = new HashMap<>();
        map.put("taskId", uploadTask.getId());
        map.put("isUpload", false);
        return map;
    }

    @ResponseBody
    @PostMapping("/upload-chunk")
    public Map<String, Object> uploadChunk(Chunk chunk) throws IOException {
        fileService.uploadChunk(chunk);

        double process = uploadTaskService.addChunkSeq(chunk.getTaskId(), chunk.getSeq());

        Map<String, Object> map = new HashMap<>();
        if (process < 1) {
            map.put("process", process);
            map.put("finished", false);
            return map;
        }

        Long taskId = chunk.getTaskId();
        UploadTask uploadTask = uploadTaskService.getUploadTask(taskId);
        fileService.mergeChunk(uploadTask);
        uploadTaskService.del(taskId);

        FileNode fileNode = new FileNode();
        fileNode.setState(0)
                .setSize(uploadTask.getSize())
                .setName(uploadTask.getName())
                .setParentId(uploadTask.getParentId())
                .setCreateTime(System.currentTimeMillis())
                .setLastModified(uploadTask.getLastModified())
                .setType(Files.probeContentType(Paths.get(fileNode.getName())))
                .setIsDir(false);
        fileNodeService.createNode(fileNode);

        FileMD5 fileMD5 = new FileMD5();
        fileMD5.setMd5(uploadTask.getMd5())
                .setNodeId(fileNode.getId());
        fileMD5Service.add(fileMD5);

        map.put("process", process);
        map.put("finished", true);
        return map;
    }

    @GetMapping("/download/{id}")
    public void download(@PathVariable("id") Long id, HttpServletResponse resp) throws IOException {
        FileNode fileNode = fileNodeService.findById(id);
        String fileMD5 = fileMD5Service.getMD5ByNodeId(fileNode.getId());
        resp.setHeader("Content-Disposition",
                "attachment;fileName=" + fileNode.getName() + "");
        resp.setContentType(fileNode.getType());
        fileService.download(fileMD5, resp);
    }
}
